package com.region.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
 
import com.common.util.ObjectUtils;
import com.core.service.AppBaseService;
import com.dpmc.entity.TB_SHGL_SHJBXX;
import com.region.entity.TB_REGION_XX;
import com.region.entity.TB_REGION_TP;

/**
 * Session Bean implementation class SVC_REGION
 */
@Stateless
public class SVC_REGION extends AppBaseService  implements SVC_REGIONRemote, SVC_REGIONLocal { 
	@Resource(name="JmsConnectionFactory")
	private ConnectionFactory connectionFactory;
 
	public Map ADD_TP(Map param) { 
		Map file = (Map) getServiceInterface().doService("SVC_UPLOADFILE", "ADD_BL_FILE", param); 
		param.put("FILE_ID", file.get("ID"));
		 
		return ADD(TB_REGION_TP.class,param);
	}

	 
	public Map UPD_TP(Map param) {
		String fileId = (String) param.get("FILE_ID");
		 
		if(fileId==null){  
			HashMap delParam = new HashMap();
			delParam.put("ID", fileId);
		 	getServiceInterface().doService("SVC_UPLOADFILE", "DEL_FILE", delParam); 
			
		 	if( param.get("FILE_NAME")!=null){
		 		Map file = (Map) getServiceInterface().doService("SVC_UPLOADFILE", "ADD_BL_FILE", param); 
				param.put("FILE_ID", file.get("ID"));
			}  
		}  
		return  UPD(TB_REGION_TP.class,param);
	}

	 

	@Override
	public List SAVE_XX_LIST(Map param) {
		List retList = new ArrayList();
		List<Map> xxList = (List) param.get("XX_LIST");
		if(xxList!=null){
			for(Map item:xxList){
				if(item.get("ID")==null){
					copySysParam(param, item);
					retList.add(ADD(TB_REGION_XX.class,item));
				}else{
					copySysParam(param, item);
					retList.add(UPD(TB_REGION_XX.class,item));
				}
			}
		}
		return retList;
	}

	@Override
	public List SAVE_TP_LIST(Map param) {
		List retList = new ArrayList();
		List<Map> tpList = (List) param.get("TP_LIST");
		if(tpList!=null){
			for(Map item:tpList){
				if(item.get("ID")==null){
					copySysParam(param, item);
				 
					retList.add(ADD_TP(item));
				}else{
					copySysParam(param, item);
					retList.add(UPD_TP(item));
				}
			}
		}
		
		
		return retList;
	}


	@Override
	public void DEL_TP(Map param) {
		String fileId = (String) param.get("FILE_ID");
		 
		if(fileId==null){  
			HashMap delParam = new HashMap();
			delParam.put("ID", fileId);
		 	getServiceInterface().doService("SVC_UPLOADFILE", "DEL_FILE", delParam); 
			 
		}  
		DEL(TB_REGION_TP.class,param); 
	}


	@Override
	public void RELEASE_INFO(Map param) {

		try {
			Connection connection = connectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(true, 0);
			Destination destination = session.createQueue("ReleaseDqxxQueue");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT); 
            
            /**锁定商户信息，避免重复提交***/
            HashMap inParam = new HashMap();
            copySysParam(param, inParam);
            inParam.put("PROVINCE_CODE", param.get("QYID"));
            inParam.put("STATUS", -10);
            getServiceInterface().doService("SVC_BASE", "UPD_PROVINCES_STATUS", inParam); 
            
            TextMessage message = session.createTextMessage(ObjectUtils.objectToJson(param));
            producer.send(message);
            connection.close(); 
			
		} catch (JMSException e) { 
			e.printStackTrace();
		} 
	}

    

}
